#include<iostream>
#include<cstring>
#include<sys/types.h>
#include<sys/stat.h>
#include<assert.h>
#include<fcntl.h>
#include<unistd.h>
#include"comm.hpp"
int main()
{

   
    umask(0);
    int n=mkfifo(fifoname.c_str(),mode);
    if(n!=0)
    {
        std::cout<<errno<<":"<<strerror(errno)<<std::endl;
        return 1;

    }
    std::cout<<"启动成功建立通信管道"<<std::endl;

    //2.服务端开启管道文件
    int rfd=open(fifoname.c_str(),O_RDONLY);
    if(rfd<0)
    {
        std::cout<<errno<<":"<<strerror(errno)<<std::endl;
        return 2;
    }
    std::cout<<"开始ipc通信:"<<std::endl;
    std::cout<<"processA:";


    //3.通信
    char buffer[NUM];
    while(true)
    {
        buffer[0]=0;
        ssize_t n=read(rfd,buffer,sizeof(buffer)-1);//当字符串看预留一个位置
        if(n>0)
        {
            buffer[n]=0;
            //std::cout<<"client# "<<buffer<<std::endl;
            printf("%c",buffer[0]);
            fflush(stdout);
        }
        else if(n==0)
        {
            std::cout<<"client quit "<<std::endl;
            break;

        }
        else
        {
            std::cout<<errno<<":"<<strerror(errno)<<std::endl;
            break;
        }
    }

    //4.关闭不要fd
    close(rfd);
    unlink(fifoname.c_str());

    return 0;

}